home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0212_Drag'n'Drop from Delphi to Win Explorer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-04  |  5.5 KB  |  231 lines

  1. {
  2. Hi Vit,
  3.  
  4. : Anybody tied d'n'd any object from Delphi App (for example TListView) to
  5. : win Explorer, DeskTop?
  6. : How my App can catch the path?
  7.  
  8. Here is a piece of code I had in my arhcives <g>
  9. It shold work on D2 too --I havent checked.
  10.  
  11. HTH
  12.  
  13. Basri,
  14. kanca@ibm.net
  15.  
  16. -------BEGIN CUT AND PASTE-------------------
  17. {
  18. This small app should answer some questions for you.  If has quite a
  19. few nifty things it does too, such as shows you how to drag and drop
  20. from the file manager, owner drawn list boxes, etc.  Run it and tell
  21. me what you think.  Make sure you adjust your file manager when it
  22. comes up so that it doesn't cover up this program.  When saving as a
  23. bitmap, remember to enter in an extension.  I havn't figured out how
  24. to find the value of the SAVE FILE AS TYPE combo box...<g>
  25.  
  26. This work was originally created by Freddy Enok Hansson (100572,2032)
  27. to help me with some of my questions. I have modified and added to it
  28. to suit my needs.  If you like what you see, drop him a note and tell
  29. him thanks.
  30.  
  31. -Pat Buchanan   73072,2743-
  32. }
  33.  
  34.  
  35. unit Pat;
  36.  
  37. interface
  38.  
  39. uses
  40.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  41.   Forms, Dialogs, ExtCtrls, StdCtrls, Buttons;
  42.  
  43. type
  44.   TForm1 = class(TForm)
  45.     BitBtn1: TBitBtn;
  46.     ListBox1: TListBox;
  47.     NumIcons: TLabel;
  48.     Label1: TLabel;
  49.     Label2: TLabel;
  50.     SaveDialog1: TSaveDialog;
  51.     procedure FormCreate(Sender: TObject);
  52.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  53.     procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  54.       Rect: TRect; State: TOwnerDrawState);
  55.     procedure ListBox1DblClick(Sender: TObject);
  56.   private
  57.     { Private declarations }
  58.   procedure WMDropFiles (var Msg: TMessage); message wm_DropFiles;
  59.   public
  60.     { Public declarations }
  61.   end;
  62.  
  63. var
  64.   Form1: TForm1;
  65.  
  66. implementation
  67.  
  68. uses ShellAPI;
  69.  
  70. {$R *.DFM}
  71.  
  72. var
  73.   Pic: TPicture;
  74.   Fname : String;
  75.   TempFile: array[0..255] of Char;
  76.   Icon : TIcon;
  77.   Drop : THandle; {Handle for Msg.wParam}
  78.  
  79.  
  80.  
  81.  
  82.  procedure TForm1.WMDropFiles(var Msg: TMessage);
  83.  var
  84.    i,K,
  85.    NumFiles, NameLength : integer;
  86.    nIconsInFile : word;
  87.    nTotal : word;
  88.  
  89.  begin
  90.  
  91.   try
  92.    screen.cursor := crHourglass;
  93.  
  94.    ListBox1.clear;
  95.    Drop := Msg.wParam;
  96.    nTotal := 0;
  97.  
  98.    {Query how many files were dropped on the app}
  99.    NumFiles := DragQueryFile(Msg.wParam, $FFFF, Nil, 0);
  100.  
  101.    for i := 0 to (NumFiles-1) do begin
  102.      NameLength := DragQueryFile(Msg.wParam, i, Nil , 0);
  103.      DragQueryFile(Msg.wParam, i, TempFile, NameLength+1);
  104.      FName := StrPas(TempFile);
  105.  
  106.      {Query how many icons existin the file (-1)}
  107.      nIconsInFile := ExtractIcon(HInstance, TempFile, $FFFF);
  108.      nTotal := nTotal + nIconsInFile;
  109.  
  110.        for K := 0 to nIconsInFile-1 do begin
  111.          {Extract the icon}
  112.          Icon.Handle := ExtractIcon(HInstance, TempFile, K);
  113.  
  114.          {Create a TPicture instance}
  115.          Pic := TPicture.Create;
  116.          {Assign the icon.handle to the Pic.icon property}
  117.          Pic.Icon := Icon;
  118.  
  119.          {Add the Filename and icon to the ListBox}
  120.          ListBox1.Items.AddObject(ExtractFileName(FName), Pic);
  121.        end;  {For K}
  122.  
  123.    end;  {For I}
  124.  
  125.        IF nTotal = 0 then
  126.            NumIcons.Caption := 'None'
  127.        ELSE
  128.            NumIcons.Caption := IntToStr(nTotal);
  129.  
  130.  
  131.    finally
  132.      screen.cursor := crDefault;
  133.  
  134.    end; {main begin}
  135. end;  {WMDropFiles}
  136.  
  137.  
  138.  
  139. procedure TForm1.FormCreate(Sender: TObject);
  140. begin
  141.   DragAcceptFiles(Handle, True);
  142.   Icon := TIcon.Create;
  143.   WinExec('winfile.exe', SW_RESTORE);
  144. end;
  145.  
  146.  
  147.  
  148. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  149. var
  150.   I: Integer;
  151. begin
  152.   DragFinish(Drop);
  153.   for I := 0 to ListBox1.Items.Count - 1 do
  154.     TPicture(ListBox1.Items.Objects[I]).Free;
  155. end;
  156.  
  157. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  158.   Rect: TRect; State: TOwnerDrawState);
  159. begin
  160.   with ListBox1.Canvas do
  161.   begin
  162.     FillRect(Rect);
  163.     Pic := TPicture(ListBox1.Items.Objects[Index]);
  164.     Draw(Rect.Left, Rect.Top + 2, Pic.Graphic);
  165.     TextOut(Rect.Left + 34, Rect.Top + 5,
  166.       ListBox1.Items[Index]);
  167.   end;
  168.  
  169. end;
  170.  
  171.  
  172.  
  173.  
  174. procedure TForm1.ListBox1DblClick(Sender: TObject);
  175. var oIcon : TPicture;
  176. var oBitmap : TBitmap;
  177.  
  178. begin
  179.  
  180.  oIcon := TPicture.create;
  181.  oBitmap := TBitMap.create;
  182.  
  183.  
  184.  IF SaveDialog1.Execute then
  185.  
  186.   WITH (Sender as TListBox) DO
  187.      begin
  188.  
  189.       oIcon.Assign(TPicture(Items.Objects[ItemIndex]));
  190.  
  191.        {---------Save as an icon---------}
  192.        IF ExtractFileExt(SaveDialog1.FileName) = '.ICO' then
  193.          begin
  194.  
  195.            {Save the icon to the specified file}
  196.            oIcon.icon.SaveToFile(SaveDialog1.Filename);
  197.  
  198.            ShowMessage(ExtractFileName(SaveDialog1.Filename) + ' has been
  199. saved as an ICON.');
  200.  
  201.          end;
  202.  
  203.        {---------Save as a bitmap---------}
  204.        IF ExtractFileExt(SaveDialog1.FileName) = '.BMP' then
  205.          begin
  206.  
  207.             {Setup the bitmap size, so that it matches the icon}
  208.             oBitmap.Width := Icon.Width;
  209.             oBitmap.Height := Icon.Height;
  210.  
  211.             { Draw Icon on Bitmap }
  212.             oBitmap.Canvas.Draw( 0, 0, oIcon.Graphic );
  213.  
  214.             {Save the bitmap to the specified file}
  215.             oBitmap.SaveToFile(SaveDialog1.Filename);
  216.  
  217.             ShowMessage(ExtractFileName(SaveDialog1.Filename) + ' has been
  218. saved as a BITMAP.');
  219.  
  220.          end;
  221.  
  222.  end;
  223.  
  224.      {Clean up after yourself}
  225.      oIcon.free;
  226.      oBitmap.free;
  227.      SaveDialog1.FileName := '';
  228.  
  229. end;
  230. end.
  231.